#!/usr/bin/env python3
# coding:utf-8

import socket
import threading

host = "localhost"
port = 6677


def async_read(sock):
    while True:
        recv = s.recv(1024)
        if not recv:
            print("服务器关闭")
            break
        print("recv:", recv.decode())
    exit(1)


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((host, port))
    task = threading.Thread(target=async_read, args=(s,), daemon=True)
    task.start()
    while True:
        data = input("请输入:")
        s.sendall(data.encode())
        if data in ("q", "exit"):
            print("退出客户端")
            break
    exit()
